﻿using UnityEngine;  
public class SmoothLookFrame : MonoBehaviour {  
	public Transform _model;
	public Transform lookAtTarget;  
//	public Transform frameTarget;  
	public float distance = 10.0f;  
	public float height = 5.0f;  
	public float damping = 2.0f;  

	private Vector3 direction;  
	private Vector3 wantedPosition;  
	bool localX = true;
	bool localZ = true;
	Vector3 lastRot;
	public Quaternion rotate;
	void Update () {  
		if (!lookAtTarget || lookAtTarget.gameObject.activeSelf == false)  
			return;  

//		direction = (transform.position - lookAtTarget.position);  
//		wantedPosition = frameTarget.position + (direction.normalized * distance);  

//		wantedPosition.y = wantedPosition.y + height;  
//		transform.position = Vector3.Lerp(transform.position, wantedPosition, damping * Time.deltaTime);  

		rotate = Quaternion.LookRotation(lookAtTarget.position - _model.position);  

		_model.rotation = Quaternion.Slerp(_model.rotation, rotate, Time.deltaTime * damping);
		Vector3 v = _model.localEulerAngles;
		if (localX) {
			v.x = lastRot.x;
		}
		if (localZ) {
			v.z = lastRot.z;
		}

		_model.localEulerAngles = v;

		lastRot = _model.localEulerAngles;
	}  
	public void Look(Transform look){
		lookAtTarget = look;
	}
}  